<script type="text/javascript">
    $(document).ready(function() {
        $("#example").dataTable();
    });
    
    function _return(pet_id) {
        //confirm("คุณแน่ใจใช่ไหมที่ต้องการลบข้อมูลนี้");
        window.location.href = "index.php?pages=return_edit&pet_id=" + pet_id;
    }

    function cancel(pet_id) {
        if (confirm("คุณแน่ใจใช่ไหมที่ต้องการ ยกเลิก ฎีกานี้")) {

            $('<div>').dialog2({
                autoOpen: true,
                title: "ยกเลิกสัญญา",
                content: "dlg_cancel.php?tblname=tbl_pet_budget&id=" + pet_id,
                id: "modal-cancel"
            });
        }
    }

//    function view_return(pet_id) {
//        $('<div>').dialog2({
//            autoOpen: true,
//            title: "ยกเลิกสัญญา",
//            content: "dlg_view_return.php?pet_id=" + pet_id,
//            id: "modal-cancel"
//        });
//
//    }
</script>

<?php
//include_once 'includes/connect.php';
//include_once 'includes/connect.php';
?>
<a href="index.php?pages=budget_edit" class="btn primary" id="add_new">เพิ่มฎีกา</a>
<table id="example" class="table table-bordered dataTable">
    <caption><br></caption>
    <thead>
        <tr>

            <th>เลขที่ฎีกา</th>
            <th>เลขที่สัญญา</th>
            <th>แผนงาน</th>
            <th>งาน</th>
            <th>จำนวนเงินที่ยืม</th>
            <th>คงเหลือ</th>
            <th>action</th>

        </tr>
    </thead>

    <tbody>
        <?php
        $db = new myDBS();

        $sql = "SELECT tbl_promise_budget.*, pet_id, pet_date, panngan3_name, panngan2_name, tbl_pet_budget.status "
                . " FROM tbl_promise_budget, tbl_pet_budget, panngan2, panngan3 "
                . " WHERE tbl_promise_budget.id = tbl_pet_budget.promise_id "
                . " AND panngan2.panngan2_id = tbl_promise_budget.panngan2_id "
                . " AND panngan3.panngan3_id = tbl_promise_budget.panngan3_id";

        //echo $sql;
        $result = $db->runSQL($sql);

        while ($rows = $db->fetch_object($result)) {
            ?>
            <tr class="<?php echo ($rows->status == "cancel")? "error":""; ?> ">

                <td><p class="text-center"><?php echo $rows->pet_id; ?></p></td>
                <td><p class="text-center"><?php echo $rows->id; ?></p></td>
                <td><?php echo $rows->panngan2_name; ?></td>
                <td><?php echo $rows->panngan3_name; ?></td>
                <td><p class="text-right"><?php echo number_format($rows->total, 2); ?></p></td>
                <td><p class="text-right"><?php echo number_format($rows->remain, 2); ?></p></td>
                <td>
                    <a href="javascript:cancel('<?php echo $rows->pet_id; ?>')"><i class="icon-ban-circle"></i></a>&nbsp;|&nbsp;
                    <a href="javascript:_return('<?php echo $rows->pet_id; ?>')"><i class="icon-refresh"></i></a>&nbsp;|&nbsp;
                    <a href="budget_print.php?pet_id=<?php echo $rows->pet_id; ?>" target="_blank"><i class="icon-print"></i></a>&nbsp;|&nbsp;
                    <a href="index.php?pages=return_view&pet_id=<?php echo $rows->pet_id; ?>"><i class="icon-list"></i></a>
                </td>
            </tr>
            <?php
        }
        ?>
    </tbody></table>